class cookiesObject
	'Session data
	public 	rawHeader
	
	'Objects
	public  xmlhttp
	
	'A place to store individual cookies
	public cookieStore	

	public property get count()
		count=cookieStore.count
	end property
	
	public property get item(name)
		item=cookieStore.item(name)
	end property	
	
	public default function init()
		'wscript.echo "Cookie handler initialising."
		set cookieStore=CreateObject("Scripting.Dictionary")	
		set init = me
		'wscript.echo "Cookie handler initialised."
	end function

	private sub class_Initialize()
		'wscript.echo "Instantiating cookiesObject."
		rawHeader="undefined"
		'wscript.echo "Instantiated cookieObject."
	end sub

	private sub class_Terminate()
		'wscript.echo "Destroyed cookiesObject."
	end sub
	
	public sub processRawCookies()
		cookieIndex=0
		responseList=split(rawHeader,vbCrLf)
		''wscript.echo vbCrLf&vbCrLf&rawHeader
		for each line in responseList
			if left(line,10)="Set-Cookie" then
				section=split(right(line,len(line)-12),";")(0)
				firstEq=inStr(section,"=")	
				variable=left(section,firstEq-1)
				value=right(section,len(section)-firstEq)
				if cookieStore.exists(variable) then 
					'wscript.echo "Amending: " & variable & "=" & value
					cookieStore.Item(variable)=value
				else
					'wscript.echo "Creating: " & variable & "=" & value
					cookieStore.add variable,value
				end if
			end if
		next
	end sub
	
	public sub updateCookies(rawHeader)
		me.rawHeader=rawHeader
		processRawCookies()	
	end sub
	
	public sub addToHeader(caller)
		caller.setRequestHeader "Cookie", "x=y"
		cookieList=cookieStore.items
		keyList=cookieStore.keys
		itemList=cookieStore.items
		for index=0 to uBound(keyList)
			'wscript.echo "Writing: " & keyList(index) & "=" & itemList(index)
			caller.setRequestHeader "Cookie",keyList(index) & "=" & itemList(index)
		next 			
	end sub
	
end class